package org.blade.personal.dao.sys.impl;

import java.util.List;
import java.util.Map;

import javax.persistence.Query;

import org.blade.personal.dao.base.BaseDao2;
import org.blade.personal.dao.base.EntityMapper;
import org.blade.personal.dao.sys.AuthorityDao;
import org.blade.personal.mode.sys.SystemAuthority;
import org.blade.personal.utils.Pager;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

@Repository("AuthorityDao")
public class AuthorityDaoImpl extends BaseDao2<SystemAuthority,Long> implements
		AuthorityDao {

	@Override
	public void addAuthority(SystemAuthority authority) {
		this.add(authority);

	}

	@Override
	public int deleteAuthority(final String ids) {
		
		String sql = " delete from SystemAuthority a where a.id in (?) ";
		Query query = super.entityManager.createQuery(sql);
		query.setParameter(1, ids);
		return query.executeUpdate();
	}

	@Override
	public void updateAuthority(final SystemAuthority authority) {
		super.update(authority);
	}

	@Override
	public SystemAuthority queryAuthority(String querySql, Object[] params) {
		Query query = entityManager.createNativeQuery(querySql);
		
		for(int i = 0 ; i< params.length; i++){
			query.setParameter(i, params[i]);
		}
		return (SystemAuthority)query.getSingleResult();
		
	}

	@Override
	public void batchUpdate(final List<SystemAuthority> authorities) {
		super.update(authorities);
		
	}

	@Override
	public void batchSave(List<SystemAuthority> authorities) {
		super.add(authorities);
	}

	@Override
	public List<Map<String, Object>> queryForMapList(String querySql,
			Object[] params) {
		
		Query query = entityManager.createNativeQuery(querySql);
		for(int i = 0 ; i< params.length; i++){
			query.setParameter(i+1, params[i]);
		}
		return (List<Map<String, Object>>)query.getResultList();
	}

	@Override
	public void pagingQuery(Pager<SystemAuthority> page, String querySql,
			Object[] params) {
		
		this.pagingQuery(page, querySql, params);
	}

	@Override
	public int deleteAuthority(String deleteSql, Object[] params) {
		Query query = entityManager.createNativeQuery(deleteSql);
		for(int i = 0 ; i< params.length; i++){
			query.setParameter(i, params[i]);
		}
		return query.executeUpdate();
	}

}
